//
//  HZTabBarController.m
//  HZ
//
//  Created by huazi on 14-8-5.
//  Copyright (c) 2014年 HZ. All rights reserved.
//

#import "HZTabBarController.h"
#import "HZNavigationController.h"
#import "MovieTheatreVC.h"
#import "ActivityVC.h"
#import "GamesVC.h"
#import "MeVC.h"
#define kcolocTabSelect  @"#db3437"
@interface HZTabBarController ()
{
   
}
@end
//@implementation UIView (Screenshot)
//
//- (UIImage *)rn_screenshot {
//    UIScreen *mainScreen = [UIScreen mainScreen];
//    CGSize windowSize = self.window.frame.size;
//    if ([mainScreen respondsToSelector:@selector(scale)]) {
//        UIGraphicsBeginImageContextWithOptions(windowSize, NO, mainScreen.scale);
//    } else {
//        UIGraphicsBeginImageContext(windowSize);
//    }
//    
//    if ([self.window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
//        [self.window drawViewHierarchyInRect:self.window.bounds afterScreenUpdates:YES];
//    } else {
//        [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
//    }
//    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
//    return screenImage;
//}
//@end
@implementation HZTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.delegate =self;
    [self setViewControllers];
       // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}
- (void)setViewControllers
{
    MovieTheatreVC *_pageHome =[[MovieTheatreVC alloc] init];
    HZNavigationController *PageHomeNav =[[HZNavigationController alloc] initWithRootViewController:_pageHome];
    
    ActivityVC *_shopCar =[[ActivityVC alloc] init];
    HZNavigationController *ShopCarNav =[[HZNavigationController alloc] initWithRootViewController:_shopCar];
    
    GamesVC *_myOrder =[[GamesVC alloc] init];
    HZNavigationController *MyOrderNav =[[HZNavigationController alloc] initWithRootViewController:_myOrder];

    MeVC *me =[[MeVC alloc] init];
    HZNavigationController *MeNav =[[HZNavigationController alloc] initWithRootViewController:me];
    
    [self setTabBarItem1:PageHomeNav];
    [self setTabBarItem2:ShopCarNav];
    [self setTabBarItem3:MyOrderNav];
    [self setTabBarItem4:MeNav];
    self.viewControllers =@[PageHomeNav,ShopCarNav,MyOrderNav,MeNav];
    
}
- (void) resetTabbarItemTextColor:(UITabBarItem *) tabBarItem{
    
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor colorWithHexString:@"#7d7d7d"], NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor colorWithHexString:kcolocTabSelect], NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateHighlighted];
}

- (void)setTabBarItem1:(UIViewController *)VC
{
    UIImage *image =[UIImage imageNamed:@"yingyuan"];
    UIImage *imageSelect =[UIImage imageNamed:@"yingyuan_choose"];
    image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    imageSelect =[imageSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *tabbaritem =[[UITabBarItem alloc] initWithTitle:@"影院" image:image  selectedImage:imageSelect];
    VC.tabBarItem =tabbaritem;
    [self resetTabbarItemTextColor:tabbaritem];
}
- (void)setTabBarItem2:(UIViewController *)VC
{
    UIImage *image =[UIImage imageNamed:@"huodong"];
    UIImage *imageSelect =[UIImage imageNamed:@"huodong_choose"];
    image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    imageSelect =[imageSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *tabbaritem =[[UITabBarItem alloc] initWithTitle:@"活动" image:image  selectedImage:imageSelect];
    VC.tabBarItem =tabbaritem;
    [self resetTabbarItemTextColor:tabbaritem];
}
- (void)setTabBarItem3:(UIViewController *)VC
{
    UIImage *image =[UIImage imageNamed:@"game"];
    UIImage *imageSelect =[UIImage imageNamed:@"game_choose"];
    image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    imageSelect =[imageSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *tabbaritem =[[UITabBarItem alloc] initWithTitle:@"游戏" image:image  selectedImage:imageSelect];
    VC.tabBarItem =tabbaritem;
    [self resetTabbarItemTextColor:tabbaritem];
}
- (void)setTabBarItem4:(UIViewController *)VC
{
    UIImage *image =[UIImage imageNamed:@"me"];
    UIImage *imageSelect =[UIImage imageNamed:@"me_choose"];
    image =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    imageSelect =[imageSelect imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *tabbaritem =[[UITabBarItem alloc] initWithTitle:@"我的" image:image  selectedImage:imageSelect];
    VC.tabBarItem =tabbaritem;
    [self resetTabbarItemTextColor:tabbaritem];
}
// Create a custom UIButton and add it to the center of our tab bar
//-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
//{
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(64*2, 0, 64, 49)];
//    [button setImage:buttonImage forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(showDisTopic) forControlEvents:UIControlEventTouchUpInside];
//    [self.tabBar addSubview:button];
//}
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
//{
//    DLog(@"%li",self.selectedIndex);
//    SelectPrevious =self.selectedIndex;
//    return YES;
//}
//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    DLog(@"%li",self.selectedIndex);
//    if (self.selectedIndex ==2) {
//        [self showDisTopic];
//        self.selectedIndex =SelectPrevious;
//    }
//    else {
//   
//    Person *_person =[Person sharePerson];
//    if (_person.isLogin)
//    {
//        
//    }
//    else
//    {
//        if (self.selectedIndex ==3||self.selectedIndex==4)
//        {
//            self.selectedIndex =SelectPrevious;
//            UIStoryboard *Login =[[StoryboardManager shareInstance] getLoginSB];
//            [self presentViewController:[Login instantiateInitialViewController] animated:YES completion:nil];
//        }
//        
//    }
//    }
//}
//
//- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
//{
//    DLog(@"%li  %@",self.selectedIndex,viewControllers);
//}
//- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
//{
//    DLog(@"%li  %@,%i",self.selectedIndex,viewControllers,changed);
//}
//- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
//{
//    DLog(@"%li  %@,%i",self.selectedIndex,viewControllers,changed);
//}
//- (void)handleMessage:(NSMutableDictionary *)dic {
//    Message *_message =[[Message alloc] initWithReceiveDic:dic];
//    [[MessageManager shareMessageManager] insertMessageToMessageTable:_message];
//    Session *_session =[[Session alloc] initWithReceiveDic:dic];
//    [[SessionManager shareSessionManager] insertOrReplaceSession:_session];
//     HZNews *news =[[HZNews alloc] initWithReceiveDic:dic];
//    if ([news.NmsgType isEqualToString:@"2"]) {
//        NSURL *downLoadUrl =[NSURL URLWithString:news.NdownloadUrl];
//        NSURLRequest *request = [NSURLRequest requestWithURL:downLoadUrl];
//        NSString *filePath = [kVoiceFileStr stringByAppendingString:[NSString stringWithFormat:@"%@other.amr",[HZDateFormatter returnNowDate]]];
//
//        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//        operation.inputStream   = [NSInputStream inputStreamWithURL:downLoadUrl];
//        operation.outputStream  = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
//        //下载进度控制
//        
//        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
//            DLog(@"is download：%f", (float)totalBytesRead/totalBytesExpectedToRead);
//       
//        }];
//        
//        
//        //已完成下载
//        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//            NSData *audioData = [NSData dataWithContentsOfFile:filePath];
//
//            news.NfilePath =[[filePath substringToIndex:filePath.length-3] stringByAppendingString:@"wav"];
//            [audioData writeToFile:filePath atomically:YES];
//            [VoiceConverter amrToWav:filePath];
//            [[HZNewsManager shareNewsManager] addNewsToNewsTable:news];
//            if (self.delelgateHandleNew) {
//                [self.delelgateHandleNew reloadNews];
//            }
//            CHYEditNewsViewController *chy =[CHYEditNewsViewController shareCHYEditNewsViewController];
//            if (chy.isAppearing &&[chy.strUserId isEqualToString:news.NuserId]) {
//                [chy getChatNewFromDatabase];
//               
//            
//            }
//                   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            
//        }];
//        
//        [operation start];
//        
//    }
//    else {
//        [[HZNewsManager shareNewsManager] addNewsToNewsTable:news];
//        if (self.delelgateHandleNew) {
//            [self.delelgateHandleNew reloadNews];
//        }
//        
//    }
//    CHYEditNewsViewController *chy =[CHYEditNewsViewController shareCHYEditNewsViewController];
//    if (chy.isAppearing &&[chy.strUserId isEqualToString:news.NuserId]) {
//        [chy getChatNewFromDatabase];
//    }
//    
//    Person *_person =[Person sharePerson];
//    NSString *strQuery =[NSString stringWithFormat:@"SELECT count(*) FROM NEWS WHERE (NEWS.oprID ='%@' AND NEWS.isNoRead='1')",_person.userId];
//    
//    NSString *strbadgeValue =[NSString stringWithFormat:@"%li",[[HZSQLManager shareSQLManager] queryNoReadNews:strQuery]];
//    HZNavigationController *nav4 =self.viewControllers[3];
//    if ([strbadgeValue integerValue] ==0) {
//       nav4.tabBarItem.badgeValue =@"";
//    }
//    else if ([strbadgeValue integerValue] <100){
//       nav4.tabBarItem.badgeValue =strbadgeValue;
//    }
//    else {
//       nav4.tabBarItem.badgeValue =@"99+";
//    }
//    [[PlayAudioManager sharePlayAudioManager] play];
//}
@end
